﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CapstoneProjectAEReader.Entities.Models;
using CapstoneProjectAEReader.Common.Utility;

namespace CapstoneProjectAEReader.Business.Manager
{
    public class EbookManager
    {
        #region Attributes

        private List<Ebook> _ebookData = new List<Ebook>();
        private List<EbookGroup> _ebookGroup = new List<EbookGroup>();

        #endregion

        #region Methods

        /// <summary>
        /// Default constructor
        /// </summary>
        public EbookManager()
        {
        }

        public EbookManager(List<Ebook> eBooks, List<EbookGroup> eGroups)
        {
            _ebookData = eBooks;
            _ebookGroup = eGroups;
        }

        public List<Ebook> EbookData
        {
            get { return _ebookData; }
            set { _ebookData = value; }
        }

        public List<EbookGroup> EbookGroup
        {
            get { return _ebookGroup; }
            set { _ebookGroup = value; }
        }

        /// <summary>
        /// Add new Ebook
        /// </summary>
        /// <param name="temp"></param>
        public void AddEbook(Ebook temp)
        {
            //0: new, 1: existed
            int status = 0;
            if (_ebookData.Any(ebook => temp.Title.Equals(ebook.Title, StringComparison.InvariantCultureIgnoreCase)
                                        && temp.Author.Equals(ebook.Author, StringComparison.InvariantCultureIgnoreCase)))
            {
                status = 1;
            }
            if (status == 1)
            {
                //ebook da ton tai
            }
            else
            {
                _ebookData.Add(temp);
            }
        }

        /// <summary>
        /// Delete Ebook
        /// </summary>
        /// <param name="temp"></param>
        public void DeleteEbook(Ebook temp)
        {
            _ebookData.Remove(temp);
        }

        /// <summary>
        /// Update Ebook information
        /// </summary>
        /// <param name="temp"></param>
        public void UpdateEbook(Ebook temp)
        {
            Ebook foundItem = _ebookData.Find(e => e.Title == temp.Title && e.Author == temp.Author);
            if (foundItem != null)
            {
                int index = _ebookData.BinarySearch(foundItem);
                _ebookData[index] = temp;
            }
            else
            {
                //E-book khong ton tai, display warning message
            }
        }

        /// <summary>
        /// Order Ebook
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        public List<Ebook> OrderEbook(int mode)
        {
            if (mode == Constants.OrderMode.AlphabeticalOrder)
            {
                return _ebookData.OrderBy(e => e.Title).ToList();
            }
            if (mode == Constants.OrderMode.TimeOrder)
            {
                return _ebookData.OrderBy(e => e.ImportDate).ToList();
            }
            return null;
        }

        /// <summary>
        /// Search Ebook by condition
        /// </summary>
        /// <param name="text"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public List<Ebook> SearchEbook(string text, int mode)
        {
            List<Ebook> rtList = new List<Ebook>();
            //mode 0: title
            if (mode == Constants.SearchMode.ByTitle)
            {
                rtList = _ebookData.FindAll(e => e.Title.Contains(text));
                return rtList;
            }
            if (mode == Constants.SearchMode.ByAuthor)
            {
                rtList = _ebookData.FindAll(e => e.Author.Contains(text));
                return rtList;
            }
            if (mode == Constants.SearchMode.ByDate)
            {
                rtList = _ebookData.FindAll(e => DateTime.Parse(e.ImportDate) < DateTime.Parse(text));
                return rtList;
            }
            return rtList;
        }

        /// <summary>
        /// Add new ebook group
        /// </summary>
        /// <param name="temp"></param>
        public void AddGroup(EbookGroup temp)
        {
            //0: new, 1: existed
            int status = 0;
            if (_ebookGroup.Any(e => e.Groupname.Equals(temp.Groupname, StringComparison.InvariantCultureIgnoreCase)))
            {
                status = 1;
            }

            if (status != 1)
            {
                _ebookGroup.Add(temp);
            }
            else
            {
                //Group da ton tai
            }
        }

        /// <summary>
        /// Update ebook group information
        /// </summary>
        /// <param name="temp"></param>
        public void UpdateGroup(EbookGroup temp)
        {
            EbookGroup foundItem = _ebookGroup.Find(e => e.Groupname == temp.Groupname);
            if (foundItem != null)
            {
                int index = _ebookGroup.BinarySearch(foundItem);
                _ebookGroup[index] = temp;
            }
            else
            {
                //Group khong ton tai, display warning message
            }
        }

        public void DeleteGroup(EbookGroup temp)
        {
            _ebookGroup.Remove(temp);
        }

        #endregion
    }
}